package com.ytjj.qmyx.admin.service.impl;

import com.github.pagehelper.util.StringUtil;
import com.ytjj.qmyx.admin.model.request.DataAnalyseRequest;
import com.ytjj.qmyx.admin.model.response.DataAnalyseResponse;
import com.ytjj.qmyx.admin.service.DataAnalyseService;
import com.ytjj.qmyx.mall.mapper.ChannelMapper;
import com.ytjj.qmyx.mall.mapper.DataAnalyseMapper;
import com.ytjj.qmyx.mall.mapper.DataModelAnalyseMapper;
import com.ytjj.qmyx.mall.model.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 
 * @version 1.0
 *
 */
@Service
public class DataAnalyseServiceImpl implements DataAnalyseService {

    @Resource
    private DataAnalyseMapper dataAnalyseMapper;

    @Resource
    private DataModelAnalyseMapper dataModelAnalyseMapper;

    @Resource
    private ChannelMapper channelMapper;

    @Override
    public DataAnalyseResponse queryDataAnalyseByChannel(DataAnalyseRequest dataAnalyseRequest) {
        DataAnalyseResponse dataAnalyseResponse = new DataAnalyseResponse();
        DataAnalyseExample dataAnalyseExample = new DataAnalyseExample();
        DataAnalyseExample.Criteria criteria = dataAnalyseExample.createCriteria();
        criteria.andCreateTimeEqualTo(dataAnalyseRequest.getCreateDate());

        ChannelExample channelExample = new ChannelExample();
        channelExample.createCriteria()
                .andChannelEqualTo(dataAnalyseRequest.getChannel());
        List<Channel> channel = channelMapper.selectByExample(channelExample);
        ChannelExample channelExample2 = new ChannelExample();
        channelExample2.createCriteria()
                .andPidEqualTo(channel.get(0).getId());
        List<Channel> channel2 = channelMapper.selectByExample(channelExample2);
        List<String> channelList = null;
        if (channel2.size() > 0) {
            channelList = Arrays.asList(dataAnalyseRequest.getChannel(), channel2.get(0).getChannel());
        } else {
            channelList = Arrays.asList(dataAnalyseRequest.getChannel());
        }
        criteria.andChannelIn(channelList);
        List<DataAnalyse> dataAnalyseList = dataAnalyseMapper.selectByExample(dataAnalyseExample);
        dataAnalyseResponse.setDataAnalyseList(dataAnalyseList);
        DataModelAnalyseExample dataModelAnalyseExample = new DataModelAnalyseExample();
        DataModelAnalyseExample.Criteria criteria1 = dataModelAnalyseExample.createCriteria();
        criteria1.andCreateTimeEqualTo(dataAnalyseRequest.getCreateDate());
        if (channel2.size() > 0) {
            channelList = Arrays.asList(dataAnalyseRequest.getChannel(), channel2.get(0).getChannel());
        } else {
            channelList = Arrays.asList(dataAnalyseRequest.getChannel());
        }
        criteria1.andChannelIn(channelList);
        List<DataModelAnalyse> dataModelAnalyses = dataModelAnalyseMapper.selectByExample(dataModelAnalyseExample);
        dataAnalyseResponse.setDataModelAnalyses(dataModelAnalyses);
        return dataAnalyseResponse;
    }




}
